package com.yonyou.cyxdms.basedata.service.service.questionaire;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyx.function.utils.common.CommonUtils;
import com.yonyou.cyxdms.basedata.service.constant.BasedataDictConstants;
import com.yonyou.cyxdms.basedata.service.dto.questionaire.QueryAnswerDTO;
import com.yonyou.cyxdms.basedata.service.dto.questionaire.QuestionDTO;
import com.yonyou.cyxdms.basedata.service.entity.questionaire.QuestionPO;
import com.yonyou.cyxdms.basedata.service.entity.questionaire.QuestionRelationPO;
import com.yonyou.cyxdms.basedata.service.repository.questionaire.QuestionMapper;
import com.yonyou.cyxdms.basedata.service.repository.questionaire.QuestionRelationMapper;
import com.yonyou.cyxdms.basedata.service.vo.questionaire.AnswerQuestionVO;
import com.yonyou.cyxdms.basedata.service.vo.questionaire.QuestionGroupVO;
import com.yonyou.cyxdms.basedata.service.vo.questionaire.QuestionVO;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 问题库 服务实现类
 * </p>
 *
 * @author renwd
 * @since 2019-09-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    QuestionRelationMapper questionRelationMapper;

    /**
     * 问题信息列表
     *
     * @param page
     * @param queryAnswerDTO
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.questionaire.QuestionGroupVO>
     * @author dengqiang
     * @since 2019/9/20
     */
    @Override
    public IPage<QuestionGroupVO> queryQuestionGroupInfo(Page<QuestionGroupVO> page, QueryAnswerDTO queryAnswerDTO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        queryAnswerDTO.setAppId(loginInfoDto.getAppId());
        queryAnswerDTO.setOwnerCode(loginInfoDto.getOwnerCode());
        page.setRecords(questionMapper.queryQuestionGroupInfo(page, queryAnswerDTO));
        return page;
    }

    /**
     * 新增问题信息
     *
     * @param questionDTO
     * @return java.lang.Integer
     * @author dengqiang
     * @since 2019/9/20
     */
    @Override
    public Integer addQuestionInfo(QuestionDTO questionDTO) {

        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        QueryWrapper<QuestionPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(QuestionPO::getAppId, loginInfoDto.getAppId())
                .eq(QuestionPO::getOwnerCode, loginInfoDto.getOwnerCode())
                .eq(QuestionPO::getQuestionName, questionDTO.getQuestionName());
        List<QuestionPO> questionPoList = questionMapper.selectList(queryWrapper);
        if (!CommonUtils.isNullOrEmpty(questionPoList)) {
            throw new ServiceBizException("该问题已经存在");
        }

        QueryWrapper<QuestionPO> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(QuestionPO::getAppId, loginInfoDto.getAppId())
                .eq(QuestionPO::getOwnerCode, loginInfoDto.getOwnerCode())
                .eq(QuestionPO::getQuestionContent, questionDTO.getQuestionContent());
        List<QuestionPO> questionPoLists = questionMapper.selectList(queryWrapper2);
        if (!CommonUtils.isNullOrEmpty(questionPoLists)) {
            throw new ServiceBizException("问题内容已存在，不允许保存");
        }
        QuestionPO questionPo = new QuestionPO();
        setQuestion(questionPo,questionDTO);
        questionPo.setCompanyCode(loginInfoDto.getCompanyCode());
        questionPo.setAppId(loginInfoDto.getAppId());
        questionPo.setOwnerCode(loginInfoDto.getOwnerCode());
        questionPo.setOwnerParCode(loginInfoDto.getOwnerParCode());
        questionPo.setDataSources(BasedataDictConstants.DATA_SOURCES_BY_OWNER);
        questionMapper.insert(questionPo);
        return 1;
    }

    /**
     * 根据id查询问题信息
     *
     * @param questionId
     * @return com.yonyou.cyxdms.basedata.service.vo.questionaire.QuestionVO
     * @author dengqiang
     * @since 2019/9/20
     */
    @Override
    public QuestionVO queryQuestionById(Long questionId) {
        return questionMapper.queryQuestionById(questionId);
    }

    /**
     * 修改问题信息
     *
     * @param questionDTO
     * @return java.lang.Integer
     * @author dengqiang
     * @since 2019/9/20
     */
    @Override
    public Integer updateQuestionInfo(QuestionDTO questionDTO) {
        QuestionPO questionPo = questionMapper.selectById(questionDTO.getQuestionId());

        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);

        List<Long> questionList = questionMapper.selectByQuestionName(loginInfoDto.getAppId(),loginInfoDto.getOwnerCode(),questionDTO.getQuestionId(),questionDTO.getQuestionName());
        if (!CommonUtils.isNullOrEmpty(questionList)) {
            throw new ServiceBizException("该问题已经存在");
        }

        List<Long> questionList2 = questionMapper.selectByQuestionContent(loginInfoDto.getAppId(),loginInfoDto.getOwnerCode(),questionDTO.getQuestionId(),questionDTO.getQuestionContent());
        if (!CommonUtils.isNullOrEmpty(questionList2)) {
            throw new ServiceBizException("问题内容已存在，不允许保存");
        }

        setQuestion(questionPo,questionDTO);
        questionPo.setRecordVersion(questionDTO.getRecordVersion());
        questionMapper.updateById(questionPo);
        return 1;
    }

    /**
     * 给问题设置属性
     *
     * @param questionPo
     * @param questionDTO
     * @return void
     * @author dengqiang
     * @since 2019/9/20
     */
    @Override
    public void setQuestion(QuestionPO questionPo, QuestionDTO questionDTO) {
        questionPo.setQuestionName(questionDTO.getQuestionName());
        questionPo.setQuestionContent(questionDTO.getQuestionContent());
        questionPo.setQuestionType(questionDTO.getQuestionType());
        questionPo.setIsMustFilled(questionDTO.getIsMustFilled());
        questionPo.setIsValid(questionDTO.getIsValid());
        questionPo.setAnswerGroupId(questionDTO.getAnswerGroupId());
    }

    /**
     * 删除问题信息
     *
     * @param questionId
     * @return java.lang.Integer
     * @author dengqiang
     * @since 2019/9/20
     */
    @Override
    public Integer deleteQuestionById(Long questionId) {
        QueryWrapper<QuestionRelationPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(QuestionRelationPO::getQuestionId, questionId);
        questionRelationMapper.delete(queryWrapper);
        QuestionPO questionPo = questionMapper.selectById(questionId);
        return questionMapper.deleteById(questionPo);
    }



    /**
     * 根据问题id查询问题和答案组信息
     *
     * @param page
     * @param questionId
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.questionaire.AnswerQuestionVO>
     * @author dengqiang
     * @since 2019/9/23
     */
    @Override
    public IPage<AnswerQuestionVO> queryAnswerQuestionByQuestionId(Page<AnswerQuestionVO> page, Long questionId) {
        page.setRecords(questionMapper.queryQuestionByQuestionnaireId(page, questionId));
        return page;
    }


}
